<?php
//首页显示
namespace Home\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
       $shop=M('shop');
    	$type=I('get.type');
    	if($type!=null){
    		session('index_type',$type);
    	}else{
    		$type=session('index_type');
    	}
    	//综合
    	if($type==0){
    		$order="id desc";
    	}
    	//人气
		if($type==1){
    		$order="click_num desc";
    	}
    	//上架时间
    	if($type==2){
    		$order="addtime desc";
    	}
    	//销量
    	if($type==3){
    		$order="sale_num desc";
    	}
    	//价格
    	if($type==4){
    		$order="price desc";
    	}
    	//拼团
    	$nowtime=time();
    	$ptuan=$shop->where("ishidden=0 and tuanselect=1 and tuanz_starttime<$nowtime and tuanz_endtime>$nowtime")->limit('6')->order('tuan_order desc')->select();
    	$this->assign('ptuan',$ptuan);
    	
    	//商品推荐
    	$recommend=$shop->where('isindex=1 and ishidden=0')->limit('9')->order($order)->select();
    	$this->assign('recommend',$recommend);
    	//猜你喜欢
  
		$num=10;
		$max=$shop->max('id');
		$min=$shop->min('id');
		
		$i=1;
		$flag=0;
		while($i<=$num){
			$rundnum=rand($min, $max);
			$flag_2=true;
			if($flag!=$rundnum){
				if($like_list){
					foreach($like_list as $key=>$value){
						if($rundnum==$value['id']){
							$flag_2=false;
						}
					}
				}
				if($flag_2){
					$rs=$shop->where('ishidden=0')->getById($rundnum);
					if(!empty($rs)){
						$like_list[]=$rs;
						$i++;
						$flag=$rundnum;
					}
				}
				
				
			}
			
		}
		
    	$this->assign('like_list',$like_list);
    	
		$this->display();
	}
	//二维码生成
	public function qrcode(){
        Vendor('phpqrcode.phpqrcode');
        //生成二维码图片
        $object = new \QRcode();
        $url=I('get.url');//网址或者是文本内容
        $level=3;
        $size=4;
        $errorCorrectionLevel =intval($level) ;//容错级别
        $matrixPointSize = intval($size);//生成图片大小
        $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
    }
    
    public function subsite(){
        if($_GET['type']==2){
            session('no_qiecity',1);
            $this->redirect('/');
        }
    	$id=I('get.id');
    	if(!$id){
            $this->redirect('/');
		}
    	$sys_city=M('sys_city');
    	$rs=$sys_city->find($id);
    	session('site_city',$rs['city_name']);
		session('site_city_id',$id);
		$perv_url=$_SERVER['HTTP_REFERER'];
		$this->redirect($perv_url);
    }
	
	public function news(){
		$top_news=M('shop_content')->field('story_title,shop_id')->where('story_title is not null and story_title!="" and story_img is not null and story_img!=""')->limit(10)->order('story_hits desc')->select();
		$this->assign('top_news',$top_news);
		$this->display();
	}
	
	public function ajax_news(){
		$page=I('get.page');
		$num=10;
		$strat=($page-1)*$num;
		$html='';
		$count=M('shop_content')->where('story_title is not null and story_title!="" and story_img is not null and story_img!=""')->count();
		$page=ceil($count/$num);
		$news=M('shop_content')->field('story_title,story_exprot,story_content,story_time,story_img,story_hits,shop_id')->where('story_title is not null and story_title!="" and story_img is not null and story_img!=""')->limit($strat,$num)->select();
		foreach($news as $key=>$value){
			
			$html=$html.'<div class=" pad bian bgwhite">
							<div class="f">
								<a href="'.U('shop/store',array('id'=>$value['shop_id'])).'" title="'.$value['story_title'].'" target="_blank">
									<img src="'.$value['story_img'].'" width="300" height="160" alt="'.$value['story_title'].'">
								</a>
							</div>
							<div class="cat-r">
								<dl>
									<dt><a href="'.U('shop/store',array('id'=>$value['shop_id'])).'" class="font9 " title="'.$value['story_title'].'">'.$value['story_title'].'</a></dt>
									<dd class="font10">发布时间:&nbsp;'.date('Y-m-d',$value['story_time']).'</dd>
									<dd>
										<a href="'.U('shop/store',array('id'=>$value['shop_id'])).'" class="font11">'.$value['story_exprot'].'</a>
									</dd>
									<dd>
										<div id="menu">
											<ul>
												<li>
													<a href="'.U('shop/store',array('id'=>$value['shop_id'])).'">查看详情</a>
													<a href="'.U('shop/store',array('id'=>$value['shop_id'])).'">点击查看</a>
												</li>
											</ul>
										</div>
									</dd>
								</dl>
							</div>
						</div>';
		}
		$data['content']=$html;
		$data['pages']=$page;
		$this->ajaxReturn($data);

					
				
		
	}
    public  function  join(){

        $this->display();
    }
    public  function  add_join(){
		$data=I('post.');

		$phone=$data['phone'];
		if($phone){
            $rs_phone=M('store_join')->where("phone='$phone' and status=0")->find();
            if($rs_phone){
                $this->ajaxReturn(2);
            }
            $data['type']='pc';
            $data['addtime']=time();

            $rs=M('store_join')->add($data);
            if($rs){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn(0);
            }
		}else{
            $this->ajaxReturn(0);
		}


    }

    function tj_user(){
        $id=I('get.id');
        session('tj_userid',$id);

        //php判断客户端是否为手机
        $agent = $_SERVER['HTTP_USER_AGENT'];
        if( !preg_match('/m.58daiyan.com/',$_SERVER['HTTP_HOST']) ){
            if( strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS")|| strpos($agent,"X11")){

                $HTTP_HOST	=	'm.58daiyan.com';
                $url	=	'http://'.$HTTP_HOST.U('shop/index',array('id'=>171));
                header("Location:".$url);
            }else{
                if(session('user_id')){
                    $this->redirect('shop/index',array('id'=>171));
                }else{
                    $this->redirect('user/register');
                }
            }
        }


    }









}
?>
